<template>

    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0">
            </el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form">
                    <h1>Hollysys</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item>
                        <el-input type="text" v-model="loginForm.username" :prefix-icon="User"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input type="password" show-password v-model="loginForm.password"
                            :prefix-icon="Lock"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="login_button" :loading="loginLoad" type="primary"
                            @click="login">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>

</template>
<script setup lang='ts'>
import { reactive, ref } from 'vue'
// 引入图标
import { User, Lock } from '@element-plus/icons-vue'
// 引入路由器
import { useRouter } from 'vue-router';
// 引入user小仓库
import userStore from '@/store/modules/user.ts'
// 引入提示框
import { ElNotification } from 'element-plus'
// 使用小仓库
let user = userStore();
// 定义路由器
let $router = useRouter();
// 登录按钮加载状态
let loginLoad = ref(false);

// 登录表单
const loginForm = reactive({
    username: "",
    password: ""
});

// 用户按钮登录
const login = async () => {
    // 开始加载
    loginLoad.value = true;
    try {
        // 确保登录成功
        await user.userLogin(loginForm);
        // 编程式导航进行跳转
        $router.push('/home');
        // 登录成功提示
        ElNotification({
            title: 'Success',
            message: '登录成功',
            type: 'success',
            duration: 1000,
        })
        // 登录成功加载完成
        loginLoad.value = false;
    } catch (error) {
        // 登录失败,加载完成
        loginLoad.value = false;
        // 登录失败提示
        ElNotification({
            title: 'Error',
            message: (error as Error).message,
            type: 'error',
            duration: 1000,
        })
    }
}

</script>
<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;

    .login_form {
        width: 80%;
        position: relative;
        top: 30vh;
        padding: 40px;
        background: url('@/assets/images/login_form.png');

        h1 {
            color: white;
            font-size: 30px;
            margin: 20px 0px;
        }

        h2 {
            color: white;
            font-size: 20px;
            margin: 20px 0px;
        }

        .login_button {
            width: 100%;
        }

    }
}
</style>